<script setup lang="ts">
import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
import { useUserStore } from '@/stores/user';
import { autoLogin } from '@/utils/auth';

const userStore = useUserStore();

onLaunch(async () => {
  console.log("App Launch");
  // 检查登录状态
  // const token = uni.getStorageSync('token');
  // const userInfo = uni.getStorageSync('token');
  const token = '';
  const userInfo = '';
  
  if (token && userInfo) {
    // 如果有登录信息，直接恢复状态
    userStore.token = token;
    userStore.userInfo = userInfo;
    userStore.userId = userInfo?.user?.userId;
    userStore.fetchUserInfo();
  } else {
    // 没有登录信息，尝试自动登录
    try {
      console.log('尝试自动登录');
      const { code } = await autoLogin();
      await userStore.login(code);
      console.log('自动登录成功');
    } catch (error) {
      console.error('自动登录失败：', error);
    }
  }
});

onShow(() => {
  console.log("App Show");
});

onHide(() => {
  console.log("App Hide");
});
</script>

<template>
  <view>
    <slot />
  </view>
</template>
